﻿using RabbitMQ.Client;
using System.Text;

namespace RabbitMQDemo.Producer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Start the producer");

            var connFactory = new ConnectionFactory
            {
                HostName = "106.53.127.56",
                Port = 5672,
                UserName = "rabbitmqdemo",
                Password = "rabbitmqdemo@test",
                VirtualHost = "rabbitmqdemo"
            };
            using (var conn = connFactory.CreateConnection())
            {
                using (var channel = conn.CreateModel())
                {
                    //死信交换机和死信队列
                    var dlxExchangeName = "dlxroutingkey_exchange";
                    channel.ExchangeDeclare(exchange: dlxExchangeName, type: "direct", durable: false, autoDelete: false, arguments: null);
                    var dlxQueueName1 = "dlx_queue1";
                    channel.QueueDeclare(queue: dlxQueueName1, durable: false, exclusive: false, autoDelete: false, arguments: null);
                    channel.QueueBind(queue: dlxQueueName1, exchange: dlxExchangeName, routingKey: "waring");
                    var dlxQueueName2 = "dlx_queue2";
                    channel.QueueDeclare(queue: dlxQueueName2, durable: false, exclusive: false, autoDelete: false, arguments: null);
                    channel.QueueBind(queue: dlxQueueName2, exchange: dlxExchangeName, routingKey: "info");
                    var dlxQueueName3 = "dlx_queue3";
                    channel.QueueDeclare(queue: dlxQueueName3, durable: false, exclusive: false, autoDelete: false, arguments: null);
                    channel.QueueBind(queue: dlxQueueName3, exchange: dlxExchangeName, routingKey: "error");

                    //常规队列
                    var queueName = "normalmessage_queue";
                    var arguments = new Dictionary<string, object>
                    {
                        { "x-message-ttl", 10000},
                        { "x-dead-letter-exchange", dlxExchangeName },
                        { "x-dead-letter-routing-key", "info" }
                    };
                    channel.QueueDeclare(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: arguments);

                    channel.BasicReturn += new EventHandler<RabbitMQ.Client.Events.BasicReturnEventArgs>((sender, e) =>
                    {
                        var message = Encoding.UTF8.GetString(e.Body.ToArray());
                        Console.WriteLine($"收到回退消息：{message}");
                    });

                    while (true)
                    {
                        Console.WriteLine("消息内容(exit退出):");
                        var message = Console.ReadLine();
                        if (message.Trim().ToLower() == "exit")
                        {
                            break;
                        }

                        var body = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish(exchange: "", routingKey: queueName, mandatory: true, basicProperties: null, body: body);
                        Console.WriteLine("消息内容发送完毕:" + message + $" {DateTime.Now}");
                    }
                }
            }
        }
    }
}